package com.lanxin.oa.approve.service;

import com.lanxin.bean.LcFlow;
import com.lanxin.bean.LcNode;
import com.lanxin.oa.approve.dao.NodeDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class NodeServiceImpl implements NodeService {

    @Autowired
    private NodeDao nodeDao;

    @Override
    public List<LcNode> selectLcNodeById(Integer flowid) {
        List<LcNode> listNode = nodeDao.selectById(flowid);
        List<LcNode> listSortNode = new ArrayList<>();
        sortNode(listNode, null, listSortNode);
        return listSortNode;
    }

    private void sortNode(List<LcNode> listNode, Integer prenodeid, List<LcNode> listSortNode) {
        for (LcNode n : listNode) {
            if (prenodeid == n.getPrenodeid() || (prenodeid != null && prenodeid.equals(n.getPrenodeid()))) {
                listSortNode.add(n);
                sortNode(listNode, n.getOid(), listSortNode);
                break;
            }
        }
    }

    @Override
    public LcFlow selectLcFlowById(Integer flowid) {
        return nodeDao.selectLcFlowId(flowid);
    }

    @Override
    public boolean deleteNode(Integer oid) {
        int count = nodeDao.selectNodeById(oid).intValue();
        if (count > 0)
            return false;
        LcNode node = nodeDao.getNodeInfo(oid);
        if (node.getPrenodeid() == null) {
            LcNode nextNode = nodeDao.getNextNode(oid);
            if (nextNode != null) {
                nextNode.setPrenodeid(null);
                nodeDao.update(nextNode);
            }
        } else {
            LcNode nextNode = nodeDao.getNextNode(oid);
            if (nextNode != null) {
                nextNode.setPrenodeid(node.getPrenodeid());
                nodeDao.update(nextNode);
            }
        }
        nodeDao.delete(node);
        return true;

    }

    public void editLcNode(LcNode node) {
        if (node.getOid() == null) {
            addNode(node);
        } else {
            updateNode(node);
        }
    }

    private void addNode(LcNode node) {
        if (node.getPrenodeid() == null) {
            LcNode oldFirstNode = nodeDao.getFirstNode(node.getParentid());
            nodeDao.add(node);
            if (oldFirstNode != null) {
                oldFirstNode.setPrenodeid(node.getOid());
                nodeDao.update(oldFirstNode);
            }
        } else {
            LcNode oldNextNode = nodeDao.getNextNode(node.getPrenodeid());
            if (oldNextNode != null) {
                oldNextNode.setPrenodeid(node.getOid());
                nodeDao.update(oldNextNode);
            }
            else {
                nodeDao.add(node);
            }
        }
    }

    @Transactional
      void updateNode(LcNode node) {
        LcNode oldNode = nodeDao.getNodeInfo(node.getOid());
        LcNode oldNextNode = nodeDao.getNextNode(node.getOid());
        int prenodeid = 0;
        if (node.getPrenodeid() != null)
            prenodeid = node.getPrenodeid().intValue();
        int oldPrenodeid = 0;
        if (oldNode.getPrenodeid() != null)
            oldPrenodeid = oldNode.getPrenodeid().intValue();
        if (prenodeid == oldPrenodeid) {
            nodeDao.update(node);
            return;
        }
        if (oldNextNode != null) {
            oldNextNode.setPrenodeid(oldNode.getPrenodeid());
            nodeDao.update(oldNextNode);
        }
        if (node.getPrenodeid() == null) {
            LcNode oldFirstNode = nodeDao.getFirstNode(node.getParentid());
            if (oldFirstNode != null) {
                oldFirstNode.setPrenodeid(node.getOid());
                nodeDao.update(oldFirstNode);
            }
        } else {
            LcNode newNextNode = nodeDao.getNextNode(node.getPrenodeid());
            if (newNextNode != null) {
                newNextNode.setPrenodeid(node.getOid());
                nodeDao.update(newNextNode);
            }
        }
        nodeDao.update(node);
    }


    @Override
    public boolean selectByNameService(String name, Integer oid) {
        LcNode dept=nodeDao.selectByName(name,oid);
        if(dept!=null){
            return true;
        }
        return false;
    }
}
